package com.hice.demo.sbdbra.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils
{
  private static Logger log = LoggerFactory.getLogger(JsonUtils.class);

  public static String getJsonWithName(Object obj)
  {
    String str = JSON.toJSONString(obj, new SerializerFeature[] { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteClassName });

    if (StringUtils.isEmpty(str)) {
      log.error(" getJson " + obj.getClass().getSimpleName() + " to String error!");
    }

    return str;
  }

  public static String getJson(Object obj)
  {
    String str = JSON.toJSONString(obj, new SerializerFeature[] { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteDateUseDateFormat });

    if (StringUtils.isEmpty(str)) {
      log.error(" getJson " + obj.getClass().getSimpleName() + " to String error!");
    }

    return str;
  }

  public static <T> T parseJson(String jp, Class<T> clazz)
  {
    return JSON.parseObject(jp, clazz);
  }

  public static <T> T jsonToObject(String jp, TypeReference<T> valueType)
  {
    return JSON.parseObject(jp, valueType, new Feature[0]);
  }
}